﻿namespace FallingRocks
{
    using System;
    using System.Threading;

    public class FallingRocksLauncher
    {
        public static void Launch(IRenderer consoleRenderer, IUserInterface userInterface)
        {
            consoleRenderer.RenderStatic(Menu.intro);
            Console.ReadKey();
            Console.Clear();
            Console.BackgroundColor = ConsoleColor.Black;
            consoleRenderer.RenderStatic(Menu.menu1, true);
            consoleRenderer.RenderStatic(Menu.menu2);
            consoleRenderer.RenderStatic(Menu.menu3);
            consoleRenderer.RenderStatic(Menu.menu4);

            while (true)
            {
                userInterface.ProcessInput();
            }
        }

        public static void MenuSelect(IRenderer consoleRenderer, IUserInterface userInterface, Thread thread)
        {
            if (Menu.selected == 1)
            {
                IUserInterface keyboard = new KeyboardInterface();
                RealEngine realEngine = new RealEngine(consoleRenderer, keyboard);

                keyboard.OnLeftPressed += (sender, eventInfo) =>
                {
                    realEngine.MovePlayerLeft();
                };

                keyboard.OnRightPressed += (sender, eventInfo) =>
                {
                    realEngine.MovePlayerRight(Console.BufferWidth);
                };

                keyboard.OnPausePressed += (sender, eventInfo) =>
                {
                    realEngine.Pause();
                };

                Console.BackgroundColor = ConsoleColor.Black;
                Console.Clear();
                thread.Abort();
                thread = new Thread(new ThreadStart(new Sound().MissionImpossibleTune));
                thread.Start();
                realEngine.Start(thread);
            }
            else if (Menu.selected == 4)
                return;
            else if (Menu.selected == 2)
            {
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Clear();

                consoleRenderer.RenderStatic(Menu.controlInfo,false);
                Console.ReadKey();
                Menu.selected = 1;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Clear();
                Launch(consoleRenderer, userInterface);
            }
        }

        public static void Main(string[] args)
        {
            // Sound
            Sound sound = new Sound();
            Thread newThread = new Thread(new ThreadStart(sound.SuperMarioBros));
            newThread.Start();

            Console.CursorVisible = false;
            Console.TreatControlCAsInput = true;    
            Console.SetWindowSize(100, 35);

            IUserInterface keyboard = new KeyboardInterface();      
            
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth = Console.WindowWidth;
            IRenderer consoleRenderer = new ConsoleRenderer();

            keyboard.OnMenuUpPressed += (sender, eventInfo) =>
            {
                Menu.MoveSelectorUp(consoleRenderer);
            };
            keyboard.OnMenuDownPressed += (sender, eventInfo) =>
            {
                Menu.MoveSelectorDown(consoleRenderer);
            };
            keyboard.OnMenuSelectPressed += (sender, eventInfo) =>
            {
                MenuSelect(consoleRenderer, keyboard, newThread);
            };
            Launch(consoleRenderer, keyboard);
        }
    }
}
